import os
import zipfile
import time
import urllib.parse


def get_file_info(filename):
    """

    :param filename: "example.pdf"
    :return: ('example', 'pdf')
    """
    file_extension = os.path.splitext(filename)[1]  # 获取文件的扩展名
    file_name = os.path.splitext(filename)[0]
    file_type = file_extension[1:]  # 去除扩展名中的点号
    return file_name, file_type


# 把文件夹打成zip包
def zip_folder(folder_path, zip_path):
    with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                arcname = os.path.relpath(file_path, folder_path)
                zip_file.write(file_path, arcname)


# 选择某些文件打成zip包
def create_zip_file(file_paths, zip_file_name):
    with zipfile.ZipFile("./" + zip_file_name, 'w') as zipf:
        for file_path in file_paths:
            file_name = file_path.split("/")[-1]
            zipf.write(file_path, arcname=file_name)


# 时间戳
def get_timestamp():
    timestamp = str(int(time.time()))
    return timestamp


def get_file_paths(directory):
    """
    获取指定目录下的所有文件路径列表
    """
    file_paths = []  # 用于存储文件路径的列表

    # 遍历目录下的所有文件和子文件夹
    for root, directories, files in os.walk(directory):
        # 将当前目录下的所有文件添加到文件路径列表中
        for filename in files:
            file_path = os.path.join(root, filename)
            file_paths.append(file_path)

    # 2img\2021200151_1712549255\6b9a224f-89d0-4273-b329-a3c708d1f9cc-51.jpg
    # 排序
    sorted_paths = list(sorted(file_paths, key=lambda path: int(path.split('-')[-1].split('.')[0])))
    updated_paths = [path.replace('\\', '/') for path in sorted_paths]
    return updated_paths


# URL编码（解码）带有 / 和 . 的get参数
def encoded_param(param):
    return urllib.parse.quote(param)


def decoded_param(param):
    return urllib.parse.unquote(param)
